<?php

namespace app\api\controller;

use app\api\service\DiyService;
use app\api\service\ConfigService;
use app\api\service\WechatService;
use app\api\validate\WechatValidate;
use app\api\service\ArticleService;
use think\response\Json;

class Index extends ApiController
{
    /**
     * 获取首页数据
     * @return Json
     */
    public function getHomeData()
    {
        $params = $this->getData([['type', 1],['limit',6]]);

        if($params['type'] == 1) {
            $data = [
                'cms' => ArticleService::getHomeList($params),
            ];
        } else {
            $data = [
                'cms' => ArticleService::getHomeList($params),
                'recommend_cms' => ArticleService::getHomeList(['limit' => 5, 'is_recommend' => 1]),
            ];
        }

        return $this->data($data);
    }

    /**
     * 获取首页样式
     * @return Json
     */
    public function getHomeStyle()
    {
        $params = $this->getData([['type', 1]]);
        $result = DiyService::detail(['name' => 'home_page', 'type' => $params['type']]);
        return $this->data($result);
    }

    /**
     * 获取全局配置
     * @return Json
     */
    public function getConfig()
    {
        return $this->data(ConfigService::getConfig($this->getData([['type', 1]])));
    }

    /**
     * 获取微信配置
     * @return Json
     */
    public function getWxJsConfig()
    {
        $params = (new WechatValidate())->checkParam('jsConfig');
        $res = WechatService::jsConfig($params);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(WechatService::getError());
        }
    }

}